﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.IO;

namespace 正则表达式学习工具
{
    public partial class Form1 : Form
    {
        public static Match _LastMatchResult;
        public Form1()
        {
            InitializeComponent();
            BindRegexOption();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int succNum = 0;
                RegexOptions regexOptions = CreateComplicatedRegexOption();
                Match match = Regex.Match(rtbTextContent.Text.Trim(), txtRegex.Text.Trim(), regexOptions);
                if (match.Success)//缓存匹配结果
                {
                    _LastMatchResult = match;
                    BindGroupsInfo();
                }

                ClearPreviousResultInfo();


                while (match.Success)
                {
                    succNum += 1;
                    rtbMatchResult.Text += match.Value + "\n";
                    match = match.NextMatch();

                }



                if (succNum > 0)
                {
                    lblSuccInfo.Text = "匹配成功!";
                    txtSuccNum.Text = succNum.ToString();
                }
                else
                {
                    lblSuccInfo.Text = "匹配失败!";
                }
            }
            catch (Exception ex)
            {
                lblErrorInfo.Text = "错误信息:" + ex.Message;
            }
            

        }
        private void ClearPreviousResultInfo()
        {
            rtbMatchResult.Text = "";
            txtSuccNum.Text = "";
            lblSuccInfo.Text = "";
            lblErrorInfo.Text = "";

        }

        private void BindRegexOption()
        {
            Type regexOption = typeof(RegexOptions);
            string[] tmpOptionName=regexOption.GetEnumNames();
            object[] tmpOptionName2 = new object[tmpOptionName.Length];
            for (int i = 0; i < tmpOptionName.Length; i++)
            {
                tmpOptionName2[i] = tmpOptionName[i];

            }
            clbRegexOptions.Items.AddRange(tmpOptionName2);
        }

        private RegexOptions CreateComplicatedRegexOption()
        {

            RegexOptions result=RegexOptions.None;
            foreach (string name in clbRegexOptions.CheckedItems)
            {
                result = result | (RegexOptions)Enum.Parse(typeof(RegexOptions), name, true);
            }
            return result;
          

        }

        private void btnSaveCase_Click(object sender, EventArgs e)
        {
            try
            {
               
                saveFileDialog.ShowDialog();

                string resultContent = string.Empty;
                string regexOptions = string.Empty;

                foreach (string name in clbRegexOptions.CheckedItems)
                {
                    regexOptions = regexOptions + "|" + name;
                }

                regexOptions = Regex.Replace(regexOptions, @"\|(.+)", "$1");

                resultContent +="正则表达式:\r\n" + txtRegex.Text + "\r\n";
                resultContent += "\r\n";
                resultContent += "正则表达式配置项:\r\n" + regexOptions + "\r\n";
                resultContent += "\r\n";
                resultContent += "匹配的文本:\r\n" + rtbTextContent.Text + "\r\n";
                resultContent += "\r\n";
                resultContent += "匹配结果:\r\n" + rtbMatchResult.Text + "\r\n";

                File.WriteAllText(saveFileDialog.FileName, resultContent);
            }
            catch(Exception ex) {
                lblErrorInfo.Text = "错误信息:" + ex.Message;
            }


            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void BindGroupsInfo()
        {
            if (_LastMatchResult == null)
            {
                return;
            }
            ddlGroups.DataSource = null;
            ddlGroups.Items.Clear();
            List<string> groupsTbl = new List<string>();
            for (int index=0;index<_LastMatchResult.Groups.Count;index++)
            {
                groupsTbl.Add(string.Format("[{0}]{1}", index, _LastMatchResult.Groups[index].Value));
            }
            ddlGroups.DataSource = groupsTbl;
           
        }

    }
}
